<?php

require( '../config.php' );

require( '../' . NOVA_PATH . 'nova.php' );

$confile = APP_ROOT . './config.php';
$lockfile = APP_ROOT . './cache/install.lock';

require( APP_ROOT.'./core/version.php' );

// 检查博客是否安装过
if ( file_exists( $lockfile ) ) {
	echo_error( '<li>XKLog 似乎已经安装过了，请勿重复安装。</li><li>如果您确实需要重装 XKLog，请先删除 cache 文件夹下的 install.lock 文件，再运行安装程序。</li>' );
}

switch( $request->step ) {
    case 1:
        echo_step_1();
    	break;
    case 2:
        echo_step_2();
    	break;
    case 3:
        echo_step_3();
    	break;
       case 4:
        echo_step_4();
    	break;
    default:
        echo_main();
}

exit;

function echo_main(){
	echo_head('欢迎来到 XKLog 世界！');
?>
	<body>
	<h1 id="logo" style="text-align: center">XKLog V<?php echo substr(XKLOG_VERSION,0,3) ?> 安装程序</h1>
	<p style="text-align: center">XKLog! Free Personal Blog System!</p>

	<h1>准备</h1>
		<p>XKLog 是一个优雅的个人信息发布平台，欢迎加入 XKLog 世界！<br />
		现在，您需要花一分钟时间进行博客安装。安装过程不能中断，请确认您现在处于无所事事的状态~ ~<br />
		如果你对 XKLog 有任何建议、想法、评论，请加入<a href="http://bbs.xklog.cn/">支持论坛</a>。</p>

	<h1>版权许可</h1>
		<p>XKLog 基于 <abbr title="GNU Public License">GPL</abbr> 通用许可协议发布。见(英文) <a href="license.txt">license.txt</a>。</p>

	<br /><center><a href="?step=1" class="button">开始安装</a></center><br />
	</body>
	</html>
<?php
}

function echo_step_1(){
	global $confile;
	echo_head( '第一步 环境检查' );
	$result = 0;
?>
	<body>
	<h1 class="step">服务器配置</h1>
	<p><?php
	echo '操作系统：'.PHP_OS.result(1,0);
	echo 'PHP版本：'.PHP_VERSION;
	if ( PHP_VERSION > 5 ) {
		result(1,1);
	} else {
		result(0,1);
		$result += 1;
	}
	echo 'MySQL数据库：';
	if ( function_exists('mysql_connect') ) {
		echo '支持'.result(1,0);
	} else {
		echo '不支持'.result(0,0);
		$result += 1;
	}
	echo 'GD库：';
	if ( function_exists('imagecreate') ) {
		echo '支持'.result(1,0);
	} else {
		echo '不支持'.result(0,0);
	}
	?></p>

	<h1 class="step">目录和文件属性</h1>
	<p><?php
	$confilewriteable = './config.php';
	if (is_writable( $confile )) {
		$confilewriteable .= result(1,0);
	} else {
		$confilewriteable .= result(0,0);
		$result += 1;
	}
	echo $confilewriteable;
	$uploaddir = APP_ROOT.'./upload/';
	$cachedir = APP_ROOT.'./cache/';
	$datadir = APP_ROOT.'./data/';
	if ( writeable($cachedir) ) {
		echo './cache/'.result(1,0);
	} else {
		echo './cache/'.result(0,0);
		$result += 1;
	}
	if ( writeable($uploaddir) ) {
		echo './upload/'.result(1,0);
	} else {
		echo './upload/'.result(0,0);
		$result += 1;
	}
	if ( writeable($datadir) ) {
		echo './data/'.result(1,0);
	} else {
		echo './data/'.result(0,0);
		$result += 1;
	}
	?></p>
	<?php if( $result != 0 ){ echo '<p style="color:red">当前的服务器环境暂时无法安装 XKLog！</p>'; } ?>
	<br /><center><a href="./" class="button">上一步</a><?php if( $result == 0 ){ echo ' <a href="?step=2" class="button">下一步</a>'; } ?></center><br />
	</body>
	</html>
<?php
}

function echo_step_2(){
	echo_head( '第二步 博客信息' );
?>
	<body>
	<form action="?step=3" method="post">
		<h1 class="step">基本配置</h1>
		<table border="0">
		<tr>
			<td align="right" width="150">数据服务器地址：</td>
			<td><input type="text" name="dbhost" size="30" value="localhost" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>一般情况下您不必修改此项</p></td>
		</tr>
		<tr>
			<td align="right">数据库用户名：</td>
			<td><input name="dbuser" type="text" size="30" value="dbuser" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>如果您不知道此项填什么，请咨询您的空间提供商</p></td>
		</tr>
		<tr>
			<td align="right">数据库密码：</td>
			<td><input name="dbpass" type="text" size="30" value="dbpass" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>如果您不知道此项填什么，请咨询您的空间提供商</p></td>
		</tr>
		<tr>
			<td align="right">数据库名称：</td>
			<td><input name="dbname" type="text" size="30" value="dbname" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>如果您不知道此项填什么，请咨询您的空间提供商</p></td>
		</tr>
		<tr>
			<td align="right">数据表前缀：</td>
			<td><input name="dbprefix" type="text" size="30" value="xklog_" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>建议使用英文字母和下划线，不要使用特殊舒符号<br />如果您需要在同一个数据库中安装多个程序副本，您必须修改此项</p></td>
		</tr>
		</table>
		<h1 class="step">管理员用户</h1>
		<table>
		<tr>
			<td align="right" width="150">管理员名称：</td>
			<td><input name="adminname" type="text" size="30" value="admin" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>输入您的大名 ^_^</p></td>
		</tr>
		<tr>
			<td align="right">管理员密码：</td>
			<td><input name="password" type="text" size="30" value="<?php echo get_random_string(8) ?>" /></td>
		</tr>
		<tr>
			<td></td>
			<td><p>输入您的前台密码。至少五位，建议八位以上。</p></td>
		</tr>
		</table>

		<center><input type="button" class="button" onclick="history.back();" value="上一步" /> <input type="submit" name="Submit" class="button" value="下一步" /></center>
	<form>
	</body>
	</html>
<?php
}

function echo_step_3(){
	global $lockfile,$confile;

	$dbhost = trim( $_POST[ 'dbhost' ] );
	$dbuser = trim( $_POST[ 'dbuser' ] );
	$dbpass = trim( $_POST[ 'dbpass' ] );
	$dbname = trim( $_POST[ 'dbname' ] );
	$dbprefix = strtolower( trim( $_POST[ 'dbprefix' ] ) );

	$admin = trim( $_POST[ 'adminname' ] );
	$password = trim( $_POST[ 'password' ] );

	if ( $dbhost == '' ){
		echo_error( '<li>您没有输入<b>数据服务器地址</b>。</li>' );
	}
	if ( $dbuser == '' ){
		echo_error( '<li>您没有输入<b>数据库用户名</b>。</li>' );
	}
	if ( $dbpass == '' ){
		echo_error( '<li>您没有输入<b>数据库密码</b>。</li>' );
	}
	if ( $dbname == '' ){
		echo_error( '<li>您没有输入<b>数据库名称</b>。</li>' );
	}
	if ( $dbprefix == '' ){
		echo_error( '<li>您没有输入<b>前缀字符</b>。</li>' );
	}

	if ( $admin == '' ){
		echo_error( '<li>您没有输入<b>管理员名称</b>。</li>' );
	}
	if ( $password == '' ){
		echo_error( '<li>您没有输入<b>管理员密码</b>。</li>' );
	}
	$chars = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n",'#','$','(',')','%','@','+','?',';','^');
	foreach($chars as $value){
		if (strpos($admin,$value) != false){
			echo_error( '<li>您输入的<b>管理员名称</b>不合法。</li>' );
		}
	}

	if ( !$link = @mysql_connect( $dbhost,$dbuser,$dbpass ) ) {
		echo_error( '<li>数据库连接失败，请检查配置信息是否正确！</li>' );
	} else {
		if ( !@mysql_select_db( $dbname, $link ) ) {
			if ( !@mysql_query("CREATE DATABASE `$dbname`", $link) ) {
				echo_error( '<li>数据库 <b>' . $dbname . '</b> 不存在，系统也无法创建。</li>' );
			}
		}
	}

	echo_head( '正在安装……' );
?>
<body>
<h1 class="step">正在安装……</h1>
	<p>正在为您安装 XKLog V<?php echo XKLOG_VERSION ?>，请稍候……<br />
	安装期间请勿刷新或关闭页面。</p>
<?php
	// 这个地方在 linux 环境下运行出错，奇怪
	//flush();
	//ob_flush();

	$blog_path = $_SERVER[ 'SCRIPT_NAME' ];
	$blog_path = str_replace( 'install/index.php', '', $blog_path);

	// 修改配置文件
	$fp = fopen($confile, 'rb');
	$content = fread($fp, filesize($confile));
	fclose($fp);
	$content = str_replace("localhost", $dbhost, $content);
	$content = str_replace("{*DB_USER*}", $dbuser, $content);
	$content = str_replace("{*DB_PWD*}", $dbpass, $content);
	$content = str_replace("{*DB_NAME*}", $dbname, $content);
	$content = str_replace("{*DB_PREFIX*}", $dbprefix, $content);
	$content = str_replace("{*APP_PATH*}", $blogpath, $content);

	file_put_contents($confile, $content);

	// 建立数据表
	$db->connect($dbhost, $dbuser, $dbpass, $dbpass, DB_PCONNECT);

	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "set`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "article`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "guestbook`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "comment`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "user`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "safelog`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "menu`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "link`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "category`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "upload`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "trackback`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "tags`" );
	$db->query( "DROP TABLE IF EXISTS `" . $dbprefix . "online`" );

	$db->query( "CREATE TABLE `" . $dbprefix . "set` (
		`blogname` varchar(50) NOT NULL,
		`description` varchar(255) NOT NULL,
		`keywords` varchar(255),
		`beian` varchar(50),
		`domain` varchar(100) NOT NULL,
		`builddate` varchar(50) NOT NULL,
		`visitnum` int default 1 NOT NULL,
		`todaynum` int default 1 NOT NULL,
		`maxperpage` int default 8 NOT NULL,
		`maxonline` int default 100 NOT NULL,
		`filterwords` text,
		`namefilter` text,
		`enable` tinyint(1) default 1 NOT NULL,
		`closereason` varchar(255),
		`regenable` tinyint(1) default 1 NOT NULL,
		`comment` int default 0 NOT NULL,
		`comnum` int default 0 NOT NULL,
		`comtimer` int default 0 NOT NULL,
		`commaxlength` int default 600 NOT NULL,
		`artmaxlength` int default 30000 NOT NULL,
		`arttype` varchar(50) NOT NULL,
		`showtype` int default 0 NOT NULL,
		`verify` int default 1 NOT NULL,
		`style` varchar(255) default 'Default' NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "article` (
		`id` int unsigned NOT NULL auto_increment,
		`title` varchar(255) NOT NULL,
		`keywords` varchar(255) default '' NOT NULL,
		`description` varchar(255) default '' NOT NULL,
		`time` int NOT NULL,
		`author` varchar(50) NOT NULL,
		`category` int NOT NULL,
		`tag` varchar(255),
		`istop` tinyint(1) NOT NULL default 0,
		`isdel` tinyint(1) NOT NULL default 0,
		`summary` text NOT NULL,
		`content` text NOT NULL,
		`read` int default 0 NOT NULL,
		`comment` int default 0 NOT NULL,
		`trackback` int default 0 NOT NULL,
		`type` varchar(10) NOT NULL,
		`address` varchar(255) default '',
		`attachment` varchar(255) default '',
		`update` tinyint(1) NOT NULL default 0,
		PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "comment` (
		`cid` int unsigned NOT NULL auto_increment,
		`articleid` int default 1 NOT NULL,
		`isdel` tinyint(1) NOT NULL default 0,
		`author` varchar(50) NOT NULL,
		`email` varchar(255) default '' NOT NULL,
		`index` varchar(255) default '' NOT NULL,
		`time` int NOT NULL,
		`comment` text NOT NULL,
		`replyuser` varchar(50) default '',
		`reply` text,
		`replytime` varchar(50) default '',
		`isshow` tinyint(1) NOT NULL default 1,
		PRIMARY KEY (cid) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "user` (
		`u_id` int unsigned NOT NULL auto_increment,
		`u_username` varchar(50) NOT NULL,
		`u_password` varchar(50) NOT NULL,
		`u_group` int default 0 NOT NULL,
		`u_isdel` tinyint(1) NOT NULL default 0,
		`u_email` varchar(255),
		`u_qqnum` varchar(255),
		`u_index` varchar(255),
		`u_ip` varchar(50),
		`u_regtime` int NOT NULL default 0,
		`u_time` int,
		PRIMARY KEY (u_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "link` (
		`lid` int unsigned NOT NULL auto_increment,
		`isdel` tinyint(1) NOT NULL default 0,
		`title` varchar(255) NOT NULL,
		`description` varchar(255),
		`address` varchar(255) NOT NULL,
		PRIMARY KEY (lid) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "safelog` (
		`sid` int unsigned NOT NULL auto_increment,
		`description` varchar(255),
		`time` int NOT NULL,
		`username` varchar(50) NOT NULL,
		PRIMARY KEY (sid) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "menu` (
		`mid` int unsigned NOT NULL auto_increment,
		`num` int NOT NULL,
		`name` varchar(50) NOT NULL,
		`title` varchar(50) NOT NULL,
		`type` int NOT NULL,
		`address` varchar(255) NOT NULL,
		PRIMARY KEY (mid) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "category` (
		`cid` int unsigned NOT NULL auto_increment,
		`pid` int unsigned default 0 NOT NULL,
		`num` int NOT NULL,
		`name` varchar(255) NOT NULL,
		`alias` varchar(255) NOT NULL,
		`description` varchar(255),
		`articlenum` int default 0 NOT NULL,
		PRIMARY KEY (cid) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "online` (	
		`o_id` int unsigned NOT NULL auto_increment,
		`username` varchar(50) default '' NOT NULL,
		`ip` varchar(50) NOT NULL,
		`time` int NOT NULL,
		PRIMARY KEY (o_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "trackback`(
		tb_id int unsigned NOT NULL auto_increment,
		tb_article_id int NOT NULL,
		tb_title varchar(255),
		tb_excerpt varchar(255),
		tb_url varchar(255) NOT NULL,
		tb_blogname varchar(255),
		tb_type tinyint(1) NOT NULL default 0,
		PRIMARY KEY (tb_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "tags`(
		t_id int unsigned NOT NULL auto_increment,
		t_name varchar(255) NOT NULL,
		t_num int NOT NULL,
		PRIMARY KEY (t_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	$db->query( "CREATE TABLE `" . $dbprefix . "upload`(
		ul_id int unsigned NOT NULL auto_increment,
		ul_filepath varchar(255) NOT NULL,
		ul_thumb varchar(255) default '' NOT NULL,
		ul_filesize int NOT NULL,
		ul_filetype varchar(50) NOT NULL default '',
		ul_fileext char(10) NOT NULL default '',
		ul_time int NOT NULL,
		ul_downloads int default 0 NOT NULL,
		PRIMARY KEY (ul_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);

	$db->query( "CREATE TABLE `" . $dbprefix . "laodao` (
		ld_id int unsigned NOT NULL auto_increment,
		ld_time int NOT NULL,
		ld_laodao varchar(255) NOT NULL,
		PRIMARY KEY (ld_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);

	$db->query( "INSERT INTO `" . $dbprefix . "laodao` (`ld_time`,`ld_laodao`) VALUES (" . time() . ",'我要开始唠叨了！')" );

	$db->query( "INSERT INTO `" . $dbprefix . "set` (`blogname`,`description`,`domain`,`builddate`,`closereason`,`filterwords`,`namefilter`,`arttype`,`style`) VALUES ('我的部落格', '不要把光阴埋葬，乘着风追，落到有永恒的地方', 'http://" . $_SERVER['SERVER_NAME'] . "/', '" . time() . "', '博客维护中……', 'Fuck|http://', '游客|客人|admin|Fuck|管理|Guest', '动态', 'default')" );

	$hello_xklog = '<p><strong>欢迎加入 XKLog 世界！</strong><br /><br />这是安装程序自动写入的一篇日志，您可以编辑或删除。<br /><br />如果您有任何意见或建议，请访问 <a href="http://www.xklog.org">星空博客</a>。</p>';

	$db->query( "INSERT INTO `" . $dbprefix . "article` (`title`,`time`,`author`,`category`,`tag`,`summary`,`content`,`read`,`comment`,`type`) VALUES ('欢迎加入 XKLog 世界', " . time() . ", '" . $admin . "', 1, '{*1*}{*2*}', '', '" . $hello_xklog . "',1,1,'动态')" );

	$db->query( "INSERT INTO `" . $dbprefix . "comment` (`articleid`,`author`,`time`,`comment`,`replyuser`,`reply`,`replytime`) VALUES (0,'Guest'," . time() . ",'这是安装程序自动写入的一条留言。','" . $admin . "','这是安装程序自动写入的一条管理员回复。', '" . time() . "')" );
	$db->query( "INSERT INTO `" . $dbprefix . "comment` (`articleid`,`author`,`time`,`comment`,`replyuser`,`reply`,`replytime`) VALUES (1,'Guest'," . time() . ",'这是安装程序自动写入的一条评论。','" . $admin . "','这是安装程序自动写入的一条管理员回复。', '" . time() . "')" );

	$db->query( "INSERT INTO `" . $dbprefix . "user` (`u_username`,`u_password`,`u_group`) VALUES ('" . $admin . "','" . strtolower( md5( $dbprefix . $password ) ) . "',8)" );

	$db->query( "INSERT INTO `" . $dbprefix . "link` (`title`,`description`,`address`) VALUES ('星空工作室','XKLog 官方网站','http://www.xklog.cn/')" );

	$db->query( "INSERT INTO `" . $dbprefix . "category` (`num`,`name`,`alias`,`description`,`articlenum`) VALUES (1,'默认分类','default','系统默认分类',1)" );

	$db->query( "INSERT INTO `" . $dbprefix . "menu` (`num`,`name`,`title`,`type`,`address`) VALUES (1,'index','首页',0,'index.php')" );
	$db->query( "INSERT INTO `" . $dbprefix . "menu` (`num`,`name`,`title`,`type`,`address`) VALUES (2,'guestbook','留言本',0,'guestbook.php')" );
	$db->query( "INSERT INTO `" . $dbprefix . "menu` (`num`,`name`,`title`,`type`,`address`) VALUES (3,'tags','标签云',0,'tags.php')" );
	$db->query( "INSERT INTO `" . $dbprefix . "menu` (`num`,`name`,`title`,`type`,`address`) VALUES (4,'online','在线列表',0,'online.php')" );

	$db->query( "INSERT INTO `" . $dbprefix . "tags` (`t_name`,`t_num`) VALUES ('XKLog',1)" );
	$db->query( "INSERT INTO `" . $dbprefix . "tags` (`t_name`,`t_num`) VALUES ('欢迎',1)" );

	@fclose(fopen($lockfile,'wb'));

	// 删除安装文件夹
	deldir( APP_ROOT . 'install/' );
	$del_fail = false;
	if( is_dir( APP_ROOT . 'install/' ) ) {
		$del_fail = true;
	}
?>
	<h1 class="step">安装完成</h1>
	<p>恭喜你，XKLog 已经安装完毕！Just enjoy it！<br />
	<?php
	if( $del_fail ) {
		echo '安装程序未能删除自身，请手动删除 install 目录以确保安全！<br />';
	}
	?>
	<br />点击 <a href="../index.php">这里</a> 前往首页。</p>
</body>
</html>
<?php
}
?>

<?php

//删除指定目录（文件夹）中的所有文件函数
function delfile( $dir ) {
	if ( is_dir( $dir ) ) {
		$dh = @opendir( $dir );//打开目录
		//列出目录中的所有文件并去掉 . 和 ..
		while ( false !== ( $file = @readdir( $dh ) ) ) {
			if( $file != "." && $file != ".." ) { 
				$fullpath = $dir . "/" . $file;
				if( !is_dir( $fullpath ) ){
					@unlink( $fullpath );//删除目录中的所有文件
				} else {
					delfile( $fullpath );
				}
			}
			@closedir( $dh );
		}
	}
}

//删除指定的目录
function deldir( $dir ){
	delfile( $dir );
	if ( is_dir( $dir ) ) {
		@rmdir( $dir );//目录必须是空的
	}
}

function echo_error( $message ){
	echo_head( '出错了！' )
?>
	<body>
	<h1 class="step">出错了！</h1>
	<ol><?php echo( $message ) ?></ol>
	<p>点击 <a href="#" onclick="javascript:history.back();">这里</a> 返回，或者到 <a href="http://bbs.xklog.org/" target="_blank">支持论坛</a> 寻求帮助。</p>
	</body>
	</html>
<?php
	exit;
}

function echo_head( $str ){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" lang = "zh-cn">
<head>
	<meta http-equiv = "content-type" content = "text/html; charset=UTF-8" />
	<meta http-equiv = "content-Language" content = "UTF-8" />
	<title>XKLog - 安装程序 - <?php echo( $str ) ?></title>
	<style>
	html { background: #f7f7f7; }
	body {background: #fff;color: #333;font-family: "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif;margin: 2em auto 2em auto;width: 700px;padding: 1em 2em;-moz-border-radius: 11px;-khtml-border-radius: 11px;-webkit-border-radius: 11px;border-radius: 11px;border: 1px solid #dfdfdf;}
	a { color: #2583ad; text-decoration: none; }
	a:hover { color: #d54e21; }
	h1 {border-bottom: 1px solid #dadada;clear: both;color: #666;font: 24px Georgia, "times New Roman", times, serif;margin: 5px 0 0 -4px;padding: 0;padding-bottom: 7px;}
	p, li {padding-bottom: 2px;font-size: 12px;line-height: 18px;}
	code { font-size: 13px; }
	ul,ol { padding: 5px; }
	ol { padding-left:20px; }
	#logo { margin: 6px 0 14px 0; border-bottom: none;}
	.step {margin: 20px 0 15px;}
	.step, th { text-align: left; padding: 0; }
	.submit input, .button, .button-secondary {font-family: "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif;text-decoration: none;font-size: 14px !important;line-height: 16px;padding: 6px 12px;cursor: pointer;border: 1px solid #bbb;color: #464646;-moz-border-radius: 15px;-khtml-border-radius: 15px;-webkit-border-radius: 15px;border-radius: 15px;-moz-box-sizing: content-box;-webkit-box-sizing: content-box;-khtml-box-sizing: content-box;box-sizing: content-box;}
	.button:hover, .button-secondary:hover, .submit input:hover {color: #000;border-color: #666;}
	.button, .submit input, .button-secondary {background: #f2f2f2 url("white-grad.png") repeat-x scroll left top;}
	.button:active, .submit input:active, .button-secondary:active {background: #eee url("white-grad-active.png") repeat-x scroll left top;}
	</style>
</head>
<?php
}

function strip_slashes( &$array ) {
	return is_array( $array ) ? array_map( 'strip_slashes', $array ) : stripslashes( $array );
}

function result( $result = 1, $output = 1 ) {
	if( $result ) {
		$text = ' ... <span style="color:blue">OK</span><br />';
		if( !$output ) {
			return $text;
		}
		echo $text;
	} else {
		$text = ' ... <span style="color:red">Failed</span><br />';
		if( !$output ) {
			return $text;
		}
		echo $text;
	}
}

function writeable( $var ) {
	$result = false;
	if ( !is_dir( $var ) ) {
		@mkdir( $var, 0777 );
	}
	if ( is_dir( $var ) ) {
		$var .= 'temp.txt';
		if ( ( $fp=@fopen( $var, 'wb' ) ) && ( @fwrite( $fp, 'XKLog' ) ) ) {
			@fclose( $fp );
			@unlink( $var );
			$result = true;
		}
	}
	return $result;
}

function createtable( $sql ) {
	$type = strtoupper( preg_replace( "/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql ) );
	$type = in_array( $type, array( 'MYISAM', 'HEAP' ) ) ? $type : 'MYISAM';
	return preg_replace( "/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql ) . ( mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=" . str_replace( '-', '', 'UTF-8' ) : " TYPE=$type" );
}
?>
